package org.argeproje.resim.ui.model;

import org.argeproje.resim.proc.Processor;
import org.argeproje.resim.proc.data.Data;
import org.argeproje.resim.proc.view.ViewerPR;
import org.argeproje.resim.property.ComboBoxPRP;
import org.argeproje.resim.property.Property;
import org.argeproje.resim.property.PropertyManager;
import org.argeproje.resim.property.StringPRP;
import org.argeproje.resim.ui.graphics.IconFigure;
import org.eclipse.swt.graphics.Image;
import org.eclipse.draw2d.ColorConstants;
import org.eclipse.draw2d.Graphics;

public abstract class ComponentItem extends Shape{
	private static final long serialVersionUID = 1;
	private static final Image COMPONENT_ICON = createImage("icons/rectangle16.gif");
	protected String _name;
	protected String _description;
    public static final int PM_IDLE = 0;
    public static final int PM_RUNNING = 1;
    public static final int PM_AFTER_RUN = 2;
	private transient PropertyManager _propertyManager;
	protected transient Processor _processor;
	protected transient IconFigure _iconFigure;
	private static final String VIEW_PROP = "ComponentItem._viewEnabled";	
	private static final String VIEW_PROP_LIST[]=new String[]{"False","True"};
	private int _viewEnabled = 0;
	private static final String TITLE_PROP = "ComponentItem._title";	
	private String _title = "Figure";
	public void setViewEnabled(int value)
	{
		_viewEnabled = value;
	}
	
	public int getViewEnabled()
	{
		return _viewEnabled;
	}
	public void setTitle(String title)
	{
		_title = title;
	}
	
	public String getTitle()
	{
		return _title;
	}	
	public void viewOutput(){
		Data[] outputs = getProcessor().getOutputs();
		for (int i = 0; i < outputs.length; i++) {
			if(outputs[i] != null){
				ViewerPR viewer = new ViewerPR();
				viewer.setInput(outputs[i]);
				viewer.setParameters(getTitle());
				viewer.process();
			}
		}
	}
	protected void initDescriptorList()
	{
		super.initDescriptorList();
		registerProperty(new ComboBoxPRP(this, VIEW_PROP, "View Output", 
				"getViewEnabled","setViewEnabled", VIEW_PROP_LIST));
		registerProperty(new StringPRP(this, TITLE_PROP, "Output View Title", "getTitle", "setTitle"));
	}
	public void setIconFigure(IconFigure iconFigure){
		_iconFigure = iconFigure;
	}
	public IconFigure getIconFigure(){
		return _iconFigure;
	}
    public void setProcessingMode(Graphics g,int pmode){
    	IconFigure iconFigure = getIconFigure();
    	switch(pmode)
    	{
    		case PM_IDLE:{
    			iconFigure.setActiveColor(ColorConstants.lightGray);
    			break;
    		}
    		case PM_RUNNING:{
    			iconFigure.setActiveColor(ColorConstants.red);
   			 	break;
   			}
    		case PM_AFTER_RUN:{
   			 	iconFigure.setActiveColor(ColorConstants.green);
   			 	break;
   			}			 		    					 		
    	}
        iconFigure.paintFigure(g);
    }
	public ComponentItem()
	{
		super();		
	}
	
	public Processor getProcessor()
	{
		updateProc();
		return _processor;
	}
		
	private PropertyManager getPropertyManager()
	{		
		if(_propertyManager==null)
		{
			_propertyManager = new PropertyManager();
		}
		return _propertyManager;
	}
	
	/*
	public void setClass(Class clsID){
		cvChildClass = clsID;
	}*/
	
	public abstract Class getComponentClass();
	
	protected void registerProperty(Property property)
	{
       descriptors.add(property.getDescriptor());
       getPropertyManager().addProperty(property);
	}	
	/*protected void registerFileSelectProperty(Object instance,String displayName,String propertyId,String getterName,String setterName)
	{
       FileSelectPropertyDescriptor lPropDesc = new FileSelectPropertyDescriptor(propertyId,displayName);       
       descriptors.add(lPropDesc); 
       
       Method mGetter=null;
       Method mSetter=null;
       
		try {
			mGetter = getComponentClass().getMethod(getterName);		
			mSetter = getComponentClass().getMethod(setterName,Object.class);
			getPropertyManager().addProperty(new Property(instance,-1,propertyId,mGetter,mSetter));
		} catch (SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}        
	}
	*/
	/*protected void registerMatrixProperty(Object instance,String displayName,KernelDA kernel,String propertyId,String getterName,String setterName)
	{
       MatrixPropertyDescriptor lPropDesc = new MatrixPropertyDescriptor(propertyId,displayName,kernel);       
       descriptors.add(lPropDesc); 
       
       Method mGetter=null;
       Method mSetter=null;
       
		try {
			mGetter = getComponentClass().getMethod(getterName);		
			mSetter = getComponentClass().getMethod(setterName,Object.class);
			getPropertyManager().addProperty(new Property(instance,-1,propertyId,mGetter,mSetter));
		} catch (SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}        
	}*/
	
	
		
	public ComponentItem(String Name,String Description)
	{
	  super();
	  _name = Name;
	  _description = Description;
	}
	
	public Object getPropertyValue(Object propertyId) {
		if(_propertyManager.isRegisteredProperty(propertyId.toString()))
		{
			return _propertyManager.getProperty(propertyId.toString()).getValue();
		}
		return super.getPropertyValue(propertyId);
	}
	
	public void setPropertyValue(Object propertyId, Object value) {
		if(_propertyManager.isRegisteredProperty(propertyId.toString()))
		{
			_propertyManager.getProperty(propertyId.toString()).setValue(value);
		}
		super.setPropertyValue(propertyId,value);
	}
	
	public String GetName(){ return _name; }
	public String GetDescription(){ return _description; }

	public Image getIcon() {
		return COMPONENT_ICON;
	}	
	
	public String toString() {
		return "Component " + hashCode();
	}
	
	/*
	protected void readObject(ObjectInputStream in) throws IOException,
	ClassNotFoundException {
		super.readObject(in);
		cvPropMgr = new PropertyManager();		
	}*/
	public abstract void initProc();
	public abstract void updateProc();
}
